from django.shortcuts import render

# Create your views here.
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Map, Pie
from pyecharts.globals import ThemeType
import random
# 创建柱状图
def create_bar_chart() -> Bar:
    bar = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK,width='800px',height='450px'))
        .add_xaxis(['北京', '天津', '河北', '山西', '内蒙古', '辽宁', '吉林', '黑龙江', '上海', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '广西', '海南', '重庆', '四川', '贵州', '云南', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆'])
        .add_yaxis("客房收入", [56.99, 7.61, 14.41, 5.24, 6.86, 12.71, 3.13, 4.34, 53.97, 35.85, 42.49, 14.18, 24.72, 14.96, 32.03, 14.14, 17.15, 16.00, 54.16, 12.51, 19.83, 11.35, 23.72, 10.15, 12.85, 1.36, 13.77, 9.56, 2.04, 1.07, 12.23], stack='total', label_opts=opts.LabelOpts(is_show=True, position='top'))
        .add_yaxis("餐饮收入", [33.13, 5.46, 19.90, 7.95, 9.02, 12.18, 2.85, 2.24, 42.50, 61.94, 60.13, 20.15, 27.36, 12.02, 48.48, 15.76, 14.36, 15.58, 57.57, 9.58, 8.43, 9.09, 22.01, 6.29, 7.97, 0.50, 15.48, 8.59, 1.71, 1.08, 8.03], stack='total', label_opts=opts.LabelOpts(is_show=True, position='top'))
        .add_yaxis("其他收入", [39.69, 3.53, 6.73, 2.96, 2.30, 6.46, 1.07, 1.27, 27.29, 23.64, 20.62, 5.57, 6.90, 2.88, 14.30, 4.89, 3.99, 4.84, 26.74, 4.85, 3.80, 3.22, 8.88, 3.98, 6.53, 0.27, 3.05, 2.99, 0.52, 0.18, 3.02], stack='total', label_opts=opts.LabelOpts(is_show=True, position='top'))
        .set_global_opts(title_opts=opts.TitleOpts(
                         pos_top="bottom",  # 设置标题位置在底部
                         pos_left="center") # 设置标题水平居中
        )
    )
    return bar

def generate_province_data_chart():
    provinces = ["北京", "天津", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "西藏", "陕西", "甘肃", "青海", "宁夏", "新疆"]
    A_grade_scenic_spots = [221, 100, 486, 261, 404, 568, 256, 402, 134, 619, 909, 657, 449, 489, 1189, 624, 518, 553, 560, 661, 87, 269, 793, 570, 471, 134, 523, 371, 160, 117, 643]
    reception_times = [1.11, 0.54, 0.58, 0.55, 0.27, 1.04, 0.26, 0.28, 0.86, 3.36, 3.24, 1.29, 1.13, 2.00, 2.31, 1.22, 1.11, 1.78, 2.07, 1.59, 0.29, 1.21, 2.56, 1.02, 1.05, 0.02, 1.06, 0.60, 0.24, 0.17, 0.62]
    ticket_income = [16.57, 5.56, 4.79, 8.43, 4.05, 8.03, 1.88, 4.62, 7.37, 26.90, 40.00, 14.99, 8.51, 19.53, 28.44, 20.63, 13.63, 22.63, 22.56, 12.29, 7.23, 10.90, 27.87, 6.73, 6.90, 0.15, 15.01, 5.04, 2.57, 2.50, 4.68]

    bar = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK,width='800px',height='450px'))
        .add_xaxis(provinces)
        .add_yaxis("A级景区数量", A_grade_scenic_spots, z=2)
        .extend_axis(
            yaxis=opts.AxisOpts(
                type_="value",
                position="right",
                axisline_opts=opts.AxisLineOpts(
                    linestyle_opts=opts.LineStyleOpts(color="blue")
                ),
            )
        )

        # .extend_axis(
        #     yaxis=opts.AxisOpts(
        #         name="门票收入/亿元",
        #         type_="value",
        #         position="right",
        #         offset=80,
        #         axisline_opts=opts.AxisLineOpts(
        #             linestyle_opts=opts.LineStyleOpts(color="red")
        #         ),
        #     )
        # )
        .set_global_opts(
            title_opts=opts.TitleOpts(pos_top="bottom", pos_left="center",
                              title_textstyle_opts=opts.TextStyleOpts(font_size=12)),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-45)),
            yaxis_opts=opts.AxisOpts(name="A级景区数量"),
        )
    )

    line1 = (
        Line()
        .add_xaxis(provinces)
        .add_yaxis("接待人次/亿人次", reception_times, yaxis_index=1, z=3,
                   label_opts=opts.LabelOpts(is_show=True, color="#FFE100"),
                   linestyle_opts=opts.LineStyleOpts(color="#FFE100"),
                   itemstyle_opts=opts.ItemStyleOpts(color="#FFE100"))
    )

    line2 = (
        Line()
        .add_xaxis(provinces)
        .add_yaxis("门票收入/亿元", ticket_income, yaxis_index=1, z=4,
                   label_opts=opts.LabelOpts(is_show=True, color="#1AED3F"),
                   linestyle_opts=opts.LineStyleOpts(color="#1AED3F"),
                   itemstyle_opts=opts.ItemStyleOpts(color="#1AED3F"))
    )

    bar.overlap(line1)
    bar.overlap(line2)

    return bar  # 返回柱状图对象


def generate_tourism_map() -> Map:
    level_data = [
        ('5A', [8, 2, 11, 9, 6, 6, 7, 6, 4, 25, 19, 12, 10, 13, 13, 14, 13, 11, 15, 8, 6, 10, 15, 8, 9, 5, 11, 6, 4, 4, 16]),
        ('4A', [71, 33, 140, 121, 136, 142, 77, 106, 69, 219, 239, 208, 104, 193, 224, 204, 165, 152, 192, 307, 28, 131, 303, 134, 129, 23, 145, 121, 38, 30, 143]),
        ('3A', [111, 45, 223, 112, 116, 344, 118, 201, 61, 277, 537, 345, 259, 249, 695, 304, 295, 367, 340, 335, 30, 84, 380, 374, 243, 59, 326, 174, 100, 50, 388]),
        ('2A', [31.0, 20.0, 111.0, 17.0, 146.0, 70.0, 46.0, 78.0, None, 98.0, 111.0, 92.0, 76.0, 34.0, 260.0, 101.0, 44.0, 22.0, 13.0, 11.0, 17.0, 43.0, 93.0, 47.0, 78.0, 33.0, 40.0, 69.0, 18.0, 31.0, 95.0]),
        ('1A', [None, None, 1, 2, None, 6, 8, 11, None, None, None, 3, None, None, 1, 1, 1, 1, None, None, None, 1, 2, 7, 12, 14, 1, 1, None, 2, 1])
    ]

    province_data = [
        ("北京市", 221), ("天津市", 100), ("河北省", 486), ("山西省", 261), ("内蒙古自治区", 404), ("辽宁省", 568), ("吉林省", 256),
        ("黑龙江省", 402), ("上海市", 134), ("江苏省", 619), ("浙江省", 909), ("安徽省", 657), ("福建省", 449), ("江西省", 489),
        ("山东省", 1189), ("河南省", 624), ("湖北省", 518), ("湖南省", 553), ("广东省", 560), ("广西壮族自治区", 661),
        ("海南省", 87), ("重庆市", 269), ("四川省", 793), ("贵州省", 570), ("云南省", 471), ("西藏自治区", 134), ("陕西省", 523),
        ("甘肃省", 371), ("青海省", 160), ("宁夏回族自治区", 117), ("新疆维吾尔自治区", 643)
    ]

    level_color = {
        "5A": "#FF6347",
        "4A": "#1E90FF",
        "3A": "#32CD32",
        "2A": "#FFD700",
        "1A": "#9370DB"
    }

    c = Map(init_opts=opts.InitOpts(theme=ThemeType.DARK,width="900px", height="600px"))
    for level, level_values in level_data:
        c.add(
            series_name=level,
            data_pair=[(x[0], level_values[idx]) for idx, x in enumerate(province_data)],
            maptype="china",
            is_map_symbol_show=False,
            is_roam=False,
            itemstyle_opts=opts.ItemStyleOpts(color=level_color[level]),
            zoom= 1.25,
        )
    c.set_global_opts(
        visualmap_opts=[
            opts.VisualMapOpts(
                max_=max([x[1] for x in province_data]),
                min_=min([x[1] for x in province_data]),
                pos_top="40%",  # 设置组件顶部距离为 10% 页面高度
                pos_left="2%"  # 设置组件左侧距离为 10% 页面宽度
            )
        ],
        legend_opts=opts.LegendOpts(
            pos_top="1%",  # 设置图例顶部距离为 10% 页面高度
            pos_left="5%",  # 设置图例左侧距离为 37% 页面宽度

        )
    )

    return c



def generate_pie_chart():
    # 定义饼图数据
    data = [
        ("北京", 1140), ("天津", 323), ("河北", 1299), ("山西", 840), ("内蒙古", 983), ("辽宁", 1111), ("吉林", 456),
        ("黑龙江", 683), ("上海", 1353), ("江苏", 2374), ("浙江", 2455), ("安徽", 1342), ("福建", 845), ("江西", 899),
        ("山东", 2212), ("河南", 977), ("湖北", 1046), ("湖南", 1202), ("广东", 2395), ("广西", 585), ("海南", 556),
        ("重庆", 676), ("四川", 848), ("贵州", 486), ("云南", 956), ("西藏", 236), ("陕西", 809), ("甘肃", 728),
        ("青海", 376), ("宁夏", 155), ("新疆", 655)
    ]

    # 创建饼图对象
    pie = (
        Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK,width='400px',height='400px'))
        .add(
            series_name="机构数量",  # 设置系列名称
            data_pair=data,  # 设置数据
            radius=["40%", "70%"],  # 设置饼图内外半径
            label_opts=opts.LabelOpts(
                position="center",  # 设置标签位置为中央
                formatter="",  # 将 formatter 设置为空字符串以移除标签
                font_size=30,  # 设置标签字体大小
                font_weight="bold",  # 设置标签字体加粗
            )
        )
        .set_series_opts(
            label_opts=opts.LabelOpts(
                is_show=False,  # 不显示标签
                font_size=30,  # 设置标签字体大小
                font_weight="bold",  # 设置标签字体加粗
                position="center"  # 设置标签位置为中央
            ),
            emphasis={
                "label": opts.LabelOpts(
                    is_show=True,  # 在强调时显示标签
                    font_size=30,  # 设置强调时标签字体大小
                )
            }
        )
        .set_global_opts(
            tooltip_opts=opts.TooltipOpts(trigger="item"),  # 设置提示框触发类型
            legend_opts=opts.LegendOpts(is_show=False),  # 设置图例不显示
        )

    )

    return pie


# Django 视图函数
def index(request):
    # 创建图表
    bar_chart = create_bar_chart()
    line_chart = generate_province_data_chart()
    map_chart = generate_tourism_map()
    pie_chart = generate_pie_chart()

    # 渲染模板
    return render(request, 'index.html', {
        'bar_chart': bar_chart.render_embed(),
        'line_chart': line_chart.render_embed(),
        'map_chart': map_chart.render_embed(),
        'pie_chart': pie_chart.render_embed(),
    })

